const path = require("path");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

var dirname = path.resolve(__dirname);

module.exports = (env, argv) => {
	var isDev = (argv.mode !="production")
	var config= {
		entry: {
			index: path.join(dirname, "src/index.js"),
			pownerwseditor: path.join(dirname, "src/components/PowerWsEditor.js"),
		},
		output: {
			filename: "[name].js",
			// publicPath: "/static/powernet/dist", //写入到index.html资源文件的根目录
			path: path.join(dirname, "dist"), //生成的文件放到的目录
		},
		mode: argv.mode,
		module: {
			rules: [
				// {
				// 	enforce: "pre",
				// 	test: /\.js$/,
				// 	exclude: /(node_modules|bower_components|vendors)/,
				// 	loader: "eslint-loader",
				// 	options: {
				// 		fix: true,
				// 	},
				// },
				{
					test: /\.js$/,
					exclude: /(node_modules|bower_components|vendors)/,
					loader: "babel-loader",
					options: {
						cacheDirectory: true,
					},
				},
				{
					test: /\.html$/,
					loader: "html-loader",
					options: {
						minimize: true,
						// removeComments: true,
						// collapseWhitespace: true,
					},
				},
				{
					test: /\.css$/,
					use: [
						{
							loader: MiniCssExtractPlugin.loader,
							options: {
								// 这里可以指定一个 publicPath
								// 默认使用 webpackOptions.output中的publicPath
								// publicPath: "./dist",
							},
						},
						"css-loader",
					],
				},
				// {
				// 	test: /\.(sa|sc)ss$/,
				// 	use: [argv.mode !== "production" ? "style-loader" : MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
				// },
				{
					test: /\.less$/,
					use: [argv.mode !== "production" ? "style-loader" : MiniCssExtractPlugin.loader, "css-loader", "less-loader"],
				},
				{
					test: /\.(jpe?g|png|gif)$/,
					loader: "file-loader",
					options: {
						outputPath: "assets/",
					},
				},
				{
					test: /\.(eot|svg|ttf|woff2?|otf)$/,
					loader: "file-loader",
					options: {
						outputPath: "assets/",
					},
				},
			],
		},
		externals:{
			// "jquery":true,
			// "$":true,
			"window":true
		},
		plugins: [
			new HtmlWebPackPlugin({
				title: "PowerNetEditor",
				template: "src/index.html",
				filename: "index.html",
				excludeChunks: ["pownerwseditor"],
				hash: false,
			}),
			new webpack.ProvidePlugin({
				$: "jquery",
				jQuery: "jquery",
				"window.$": "jquery",
				"window.jQuery": "jquery",
				// Waves: "node-waves",
				// _: "underscore",
				// Promise: "es6-promise",
				// Popper: ["popper.js", "default"],
				L: "leaflet/dist/leaflet-src.esm",
			}),
			new MiniCssExtractPlugin({
				filename: argv.mode !== "production" ? "[name].css" : "[name].css",
				chunkFilename: argv.mode !== "production" ? "[name].css" : "[name].css",
				cssProcessorOptions: {
					safe: false,
					discardComments: {
						removeAll: true,
					},
				},
			}),
			// new CopyWebpackPlugin([
			// 	{
			// 		from: "**/*",
			// 		to: "mdb-addons",
			// 		context: path.resolve(__dirname, "src", "vendors", "mdb", "mdb-addons"),
			// 	},
			// ]),
			new CleanWebpackPlugin(),
		],
		optimization: {
			// splitChunks: {
			// 	chunks: "all",
			// },
			minimizer: [
				new TerserPlugin({
					parallel: true,
					sourceMap: true,
					terserOptions: {
						output: {
							comments: false,
						},
					},
				}),
				new OptimizeCSSAssetsPlugin({}),
			],
		},
		performance: {
			hints: false,
		},
		devServer: {
			contentBase: "/static/powernet/dist",
			proxy: {
				"/static": {
					target: "http://localhost:7001",
					changeOrigin: true,
				},
			},
			port: 8090,
		},
	};
	if(isDev) {
		config.devtool = "inline-source-map"
	}
	return config
};
